import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ApiProvider } from '../../providers/api/api'
import { Md5 } from 'ts-md5/dist/md5';
import { Storage } from '@ionic/storage';

import { PublicCompoentsProvider } from '../../providers/public-compoents/public-compoents';


/**
 * Generated class for the UserBindBankPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-user-bind-bank',
    templateUrl: 'user-bind-bank.html',
})
export class UserBindBankPage {
    public resData: any = {};
    public uid: any;
    public bankArr: Array<any>;
    public myBankMes: any = {};
    public bankId: String = '';
    public bankNum: String = '';
    public name: String = '';
    public isBind: boolean = true;
    public bankName: string;


    constructor(
        public navCtrl: NavController, 
        public navParams: NavParams,
        public pubCom: PublicCompoentsProvider,
        public Api: ApiProvider,
        public storage: Storage ) {

        storage.get('userId').then(
            (value: string) => {
                this.uid = value
            }
        );
    }

    ionViewDidEnter() {
        this.CheckBank();
        this.PostBanks();

    }

    // 选择的银行
    switch (){
        console.log( this.bankId )
    }

    // 点击绑定
    bind(){
        if ( this.uid ) {
            if ( this.name.length != 0 && this.bankId != '' && this.bankNum.length >= 19 ) {
                let timeStamp = (new Date()).getTime();
                this.Api.addBank({
                    times: timeStamp,
                    token: Md5.hashStr( 'HalfPriceMall' + timeStamp ),
                    openingbank: this.bankId,
                    blanknum: this.bankNum,
                    blankname: this.name,
                    id: this.uid
                }).then(res=>{
                    this.resData = res;
                    if( this.resData.code == 1 ) {
                        this.pubCom.toast( this.resData.msg, 'middle');
                        this.navCtrl.pop();
                    } else {
                        this.pubCom.toast( this.resData.msg, 'middle')
                    }
                })
            } else {
                this.pubCom.toast('请仔细核对信息', 'bottom');
            }
        }
        
    };

    // 输入银行卡号
    enter(val){
        this.bankNum = val;
    }

    // 获取银行卡列表
    PostBanks(){
        let timeStamp = (new Date()).getTime();
        this.Api.banklist({
            times: timeStamp,
            token: Md5.hashStr('HalfPriceMall' + timeStamp),
            banklist: 'banklist'
        }).then(res=>{
            this.resData = res;
            this.bankArr = this.resData.data;
        })
    };

    // 查看绑定的银行卡信息
    CheckBank(){
        let timeStamp = (new Date()).getTime();
        if ( this.uid ) {
            this.Api.isBindBank({
                times: timeStamp,
                token: Md5.hashStr( 'HalfPriceMall' + timeStamp ),
                userid: this.uid
            }).then(res=>{
                this.resData = res;
                console.log( res )
                if ( this.resData.code == 1 ) {
                    if ( this.resData.data.isblank != 0 ) {
                        // 有绑定银行卡信息，直接读取
                        this.isBind = false; // 隐藏绑定按钮
                        this.myBankMes = this.resData.data;
                        this.name = this.myBankMes.blankname;
                        this.bankNum = this.myBankMes.blanknum;
                    }
                }
            })
        }
    }


}
